package com.demo.controller;

import com.demo.common.CreateDate;
import com.demo.common.CreateUUID;
import com.demo.common.GetPCINFO;
import com.demo.common.ShaEncrypt;
import com.demo.model.*;
import com.demo.service.*;
import javafx.scene.control.Tab;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class IndexController {

    @Resource
    private SysManagersService managers_service;
    @Resource
    private SysDicService dic_service;

    @Resource
    private SysManagersLoginInfoService info_service;

    @Resource
    private TabMoneyShopsService shop_service;

    @Resource
    private TabUsersService user_service;

    @Resource
    private TabUsersShopsService user_shop_service;

    @Resource
    private SysManagersWrongLoginHistoryService wronghis_service;

    @RequestMapping(value = "total_manager")
    public String total_manager(HttpServletRequest request){
        int managers_num=managers_service.total_managers();
        request.setAttribute("managers_num",managers_num);

        int user_num=user_service.total_users();
        request.setAttribute("user_num",user_num);

        int shops_num=shop_service.total_shops();
        request.setAttribute("shops_num",shops_num);

        int shops_isv_num=shop_service.total_isv_shops();
        request.setAttribute("shops_isv_num",shops_isv_num);

        int login_num=info_service.total_login();
        request.setAttribute("login_num",login_num);

        int shops_users_num=user_shop_service.total_shop_users();
        request.setAttribute("shops_users_num",shops_users_num);



        return "index/welcome";
    }


    private static  final String  DEMO_INFO_BACK =   "demo_info_back";

    @RequestMapping(value = "managers_info")
    public String managers_info(String msg,HttpServletRequest request){
        String loginname = (String) request.getSession().getAttribute("loginname");
        List<SysManagers> list = managers_service.findByLoginname(loginname);
        request.setAttribute("list",list);
        request.setAttribute("b",msg);
        return "index/sys_managers/managers_info";
    }

    @RequestMapping(value = "managers_info_update")
    public String managers_info_update(SysManagers s,HttpServletRequest request){
        boolean b = managers_service.update_tel(s);
        return "redirect:managers_info?msg="+b;
    }

    @RequestMapping(value = "managers_password_pre")
    public String managers_password_pre(){
        return "index/sys_managers/managers_pwd";
    }
    @RequestMapping(value = "managers_password_pre_update")
    public String managers_password_pre_update(String oldpwd,String password,HttpServletRequest request){
        String loginname = (String) request.getSession().getAttribute("loginname");
        SysManagers sm = new SysManagers();
        sm.setPassword(ShaEncrypt.shaEncode(oldpwd));
        sm.setLoginname(loginname);
        List<SysManagers> check_list_login = managers_service.select_check_login(sm);
        if(check_list_login.isEmpty()){
            request.setAttribute("b","error");
        }else{
            SysManagers s = new SysManagers();
            s.setPassword(ShaEncrypt.shaEncode(password));
            s.setLoginname(loginname);
            boolean b = managers_service.update_password(s);
            request.setAttribute("b",b);
        }
        return "index/login";
    }



    @RequestMapping(value = "welcome")
    public String welcome(HttpServletRequest request){



        return "index/welcome";
    }


    @RequestMapping(value = "index")
    public String index(HttpServletRequest request){

        String demo_info_back_session = (String)request.getSession().getAttribute("demo_info_back");
        String managers_loginname = (String) request.getSession().getAttribute("loginname");
        request.setAttribute("demo_info_back_session",demo_info_back_session);
        request.setAttribute("managers_loginname",managers_loginname);
        return "index/index";
    }




    @RequestMapping(value = "login")
    public String login(HttpServletRequest request){
        SysDic d = dic_service.findBykeys(DEMO_INFO_BACK);
        request.setAttribute("d",d);
        return "index/login";
    }


    @RequestMapping(value = "lock_users")
    public String lock_users(String loginname) {

        List<SysManagers> list=managers_service.find_check_loginname(loginname);
        String id=list.get(0).getId();
        SysManagers s=new SysManagers();
        s.setId(id);
        s.setFlag("1");
        managers_service.update_flag(s);
        return "index/login";

    }

    @RequestMapping(value = "managers_check_login")
    public String managers_check_login(String loginname, String password, HttpServletRequest request) throws SocketException, UnknownHostException, ParseException {
        String msg = "";
        SysManagers s = new SysManagers();
        s.setLoginname(loginname.trim());
        s.setPassword(ShaEncrypt.shaEncode(password));
        if(managers_service.find_check_loginname(loginname).isEmpty()){
            msg="error";
        }else {
            request.setAttribute("na",loginname);
            List<SysManagers> check_login = managers_service.select_check_login(s);
            if(check_login.isEmpty()){
                //切入代码片段
                LoginInfoSave(loginname,"1");
                //先查出错历史里有没有，有就继续增加一个，没有就创建一个
                List<SysManagersWrongLoginHistory> wronghis_list=wronghis_service.find_wrong_by_loginname(loginname);
                //非法用户
                msg = "error";
                if(wronghis_list.isEmpty()) {
                    //新建一个
                    SysManagersWrongLoginHistory wronghis = new SysManagersWrongLoginHistory();
                    wronghis.setId(CreateUUID.getuuid());
                    wronghis.setD1(System.currentTimeMillis());
                    wronghis.setLoginname(loginname);
                    wronghis_service.save_new_wrong_by_loginname(wronghis);
                    //非法用户
                    msg = "error";
                }else {
                    if(wronghis_list.get(0).getD2()==0&&wronghis_list.get(0).getD3()==0){
                        SysManagersWrongLoginHistory wronghis = new SysManagersWrongLoginHistory();
                        wronghis.setId(CreateUUID.getuuid());
                        wronghis.setD1(0);
                        wronghis.setD2(System.currentTimeMillis());
                        wronghis.setD3(0);
                        wronghis.setLoginname(loginname);
                        wronghis_service.update_new_wrong_by_loginname(wronghis);
                        //非法用户
                        msg = "error";
                    }
                    else{
                        if(wronghis_list.get(0).getD3()==0){
                            SysManagersWrongLoginHistory wronghis = new SysManagersWrongLoginHistory();
                            wronghis.setId(CreateUUID.getuuid());
                            wronghis.setD1(0);
                            wronghis.setD3(System.currentTimeMillis());
                            wronghis.setD2(0);
                            wronghis.setLoginname(loginname);
                            wronghis_service.update_new_wrong_by_loginname(wronghis);

                            List<SysManagersWrongLoginHistory> wronghis_list_inner=wronghis_service.find_wrong_by_loginname(loginname);
                            long a = wronghis_list_inner.get(0).getD1();
                            long b = wronghis_list_inner.get(0).getD2();
                            long c = wronghis_list_inner.get(0).getD3();
                            long d13 = c - a ;
                            long d23 = c - b ;

                            System.out.println("31之间的秒数差为："+d13/1000);
                            System.out.println("1："+a);
                            System.out.println("3："+c);

                            if(d13/1000>60){
                                if(d23/1000>60){
                                    SysManagersWrongLoginHistory wronghis_change = new SysManagersWrongLoginHistory();
                                    wronghis_change.setD1(c);
                                    wronghis_change.setD2(0);
                                    wronghis_change.setD3(0);
                                    wronghis_change.setLoginname(loginname);
                                    wronghis_service.change_new_wrong_by_loginname(wronghis_change);
                                    System.out.println("1删去");
                                }else{
                                    SysManagersWrongLoginHistory wronghis_change = new SysManagersWrongLoginHistory();
                                    wronghis_change.setD1(b);
                                    wronghis_change.setD2(c);
                                    wronghis_change.setD3(0);
                                    wronghis_change.setLoginname(loginname);
                                    wronghis_service.change_new_wrong_by_loginname(wronghis_change);
                                    System.out.println("12删去");
                                }
                            }

                            //非法用户
                            msg = "error";

                        }else {
                            msg="账户被锁定，请联系管理员";
                        }
                    }

                }

            }else{
                //正常用户
                String logname = "";
                for(SysManagers m:check_login){
                    logname = m.getLoginname();
                }

                List<SysDic> dic_list = dic_service.selectAll();
                for(SysDic d:dic_list){
                    request.getSession().setAttribute(d.getDickeys(),d.getDicdes());
                }
                request.getSession().setAttribute("loginname",logname);
                request.getSession().setMaxInactiveInterval(18200);
                //切入代码片段
                LoginInfoSave(loginname,"0");

                msg = "success";
            }
        }
        request.setAttribute("msg",msg);
        return "index/login";
    }

    /**
     * 陆双代码切入
     * 主要完成工作为记录登录状态及登录信息和未来进行登录统计数据应用
     * 2021-11-20
     * */
    public void LoginInfoSave(String loginname,String flag) throws SocketException, UnknownHostException {
        String id = CreateUUID.getuuid();
        InetAddress ia = GetPCINFO.getIP();
        String ip = ia.toString();
        String mac = GetPCINFO.getLocalMac(ia);
        String datetimes = CreateDate.getDateTimes();
        SysManagersLoginInfo info = new SysManagersLoginInfo();
        info.setId(id);
        info.setLoginname(loginname);
        info.setFlag(flag);
        info.setIp(ip);
        info.setMac(mac);
        info.setLogintimes(datetimes);
        info_service.save(info);

    }




}
